﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BidFunLib.BidWeb;
using BidFunLib.BidWeb.Sale;
using System.Windows.Forms;

namespace BidFunLib.BidWeb.Sale
{
    public class BidWebSale : BidWebSaleEngine
    {
        internal Int32 verifTick;
  
        public BidWebSale(String url) : base(url)
        {
            verifTick = 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override Boolean checkForNewBid()
        {
            //Pour test
            /*
            if (Sale.BidloggerList.Count == 3)
            {
                Running = false;
                IsEnded = true;
                this.BidSaleWebEnded();
                return false;
            }*/

            if (IsEnded == true)
            {
                MyTimer.Interval = normalTickTime;
                return false;
            }

            String amount = HAmount.InnerText;
            Span_time = HTimeLeft.InnerText;

            if (Span_time == "00:00:01")
            {
                MyTimer.Interval = quickTickTime;
            }

            if (Span_time == BidVerifString)
            {
                verifTick++;
                if (FirstVerification == TIME_NULL)
                {
                    FirstVerification = DateTime.Now;
                }

                if (verifTick > MaxVerifCpt / 2) // 10 sec
                {
                    HtmlElement test =  Document.GetElementById(bid_span_time + Sale.BidFunIdentifier);
                    if (test == null || test.InnerText == null)
                    {
                        Log.Info("Possible vente sans winner Test HTimeLeft ... ");
                        BidEndedTime = DateTime.Now;
                        IsEnded = true;
                        BidSaleWebEnded();
                        return false;
                    }
                }

                if (verifTick > MaxVerifCpt) // 20 sec
                {
                    Log.Info("Possible vente sans winner Test MaxVerifCpt... ");
                    BidEndedTime = DateTime.Now;
                    IsEnded = true;
                    BidSaleWebEnded();
                    return false;
                }
            }

            if (Span_time == BidEndedString)
            {
                //Console.WriteLine(">>> " + BidEndedString);
                BidEndedTime = DateTime.Now;
                verifTick = 0;
                MyTimer.Interval = normalTickTime;
                BidSaleWebEnded();
                return false;
            }
          
            if (amount == "€0.00")
            {
                MyTimer.Interval = normalTickTime;
                return false;
            }
            else if (amount != LastBidAmount)
            {
                MyTimer.Interval = normalTickTime;
                FirstVerification = TIME_NULL;
                verifTick = 0;
                return true;
            }
            return false;
        }

        public override Boolean postInitElement()
        {
            return true;
        }


        /// <summary>
        /// On ByPass la verif pour le sniffer
        /// </summary>
        /// <returns></returns>
        public override bool verifElementInDom()
        {
            return true;
        }
    }
}
